package helloWorld;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;

import org.mt4j.MTApplication;
import org.mt4j.components.TransformSpace;
import org.mt4j.components.visibleComponents.font.FontManager;
import org.mt4j.components.visibleComponents.widgets.MTTextArea;
import org.mt4j.components.visibleComponents.widgets.buttons.MTSvgButton;
import org.mt4j.components.visibleComponents.widgets.keyboard.MTKeyboard;
import org.mt4j.sceneManagement.AbstractScene;
import org.mt4j.util.MTColor;
import org.mt4j.util.camera.MTCamera;
import org.mt4j.util.math.Vector3D;

import processing.core.PGraphics;

public class InlogScene extends AbstractScene {
	private MTApplication app;

	public InlogScene(MTApplication mtAppl, String name) {
		super(mtAppl, name);
		this.app = mtAppl;
		final MTCamera camManager = new MTCamera(mtAppl);
		this.setSceneCam(camManager);
		this.getSceneCam().setZoomMinDistance(80);

		this.setClearColor(new MTColor(0, 0, 0));

		MTKeyboard keyBoard = new MTKeyboard(app);
		keyBoard.setFillColor(new MTColor(50, 50, 50, 190));
		keyBoard.setPositionGlobal(new Vector3D(app.getWidth() / 2f, app
				.getHeight() / 2f));

		MTTextArea textArea = new MTTextArea(app, FontManager.getInstance()
				.createFont(app, "arial.ttf", 50, new MTColor(0, 0, 0, 255), // Fill
																				// color
						new MTColor(0, 0, 0, 255))); // Stroke color
		textArea.setStrokeColor(new MTColor(0, 0, 0, 255));
		textArea.setFillColor(new MTColor(255, 255, 255, 150));
		textArea.snapToKeyboard(keyBoard);
		keyBoard.setTextInputAcceptor(textArea);
		getCanvas().addChild(0, keyBoard);

		MTTextArea inlogText = new MTTextArea(app, FontManager.getInstance()
				.createFont(app, "arial.ttf", 50, new MTColor(0, 0, 0, 255), // Fill
																				// color
						new MTColor(0, 0, 0, 255))); // Stroke color
		inlogText.setStrokeColor(new MTColor(255, 255, 255));
		inlogText.setFillColor(new MTColor(255, 255, 255));
		inlogText.setText("Please enter your username");
		inlogText.setPositionGlobal(new Vector3D(400, 50));
		getCanvas().addChild(inlogText);

		MTSvgButton redButton = new MTSvgButton(System
				.getProperty("user.dir")
				+ File.separator
				+ "examples"
				+ File.separator
				+ "helloWorld"
				+ File.separator + "redbutton.svg",
				app);
		redButton.scale(0.4f, 0.4f, 1, new Vector3D(0, 0, 0),
				TransformSpace.LOCAL);
		redButton.translate(new Vector3D(350, 450, 0));
//		redButton.setBoundsPickingBehaviour(AbstractShape.BOUNDS_ONLY_CHECK);

		// Add actionlistener to flickr button
		redButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				getMTApplication().changeScene(getMTApplication().getScene("LetterScene"));
			}
		});
		getCanvas().addChild(redButton);

	}

	@Override
	public void drawAndUpdate(PGraphics graphics, long timeDelta) {
		super.drawAndUpdate(graphics, timeDelta);
	}

	@Override
	public void init() {
		app.registerKeyEvent(this);
	}

	@Override
	public void shutDown() {
		app.unregisterKeyEvent(this);
	}

	public void keyEvent(KeyEvent e) {
		int evtID = e.getID();
		if (evtID != KeyEvent.KEY_PRESSED)
			return;

		switch (e.getKeyCode()) {
		case KeyEvent.VK_BACK_SPACE:
			app.popScene();
		case KeyEvent.VK_F:
			System.out.println("FPS: " + app.frameRate);
			break;
		default:
			break;
		}
	}
}
